import java.io.*;

/** @author Chris Howson & Steve Horsman */
class MatrixArray extends Matrix
{
	/**Initiates the 2D array to store the matrix in*/
	private double[][] grid;
    
	/**Constructor*/
    public MatrixArray(){}
    /**Constructor*/
    public MatrixArray(int rows, int cols)
    {
        grid = new double[rows][cols];
        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < cols; j++)
            {
                grid[i][j] = 0;
            }
        }
        setRows(rows);
        setCols(cols);
        setType("array");
    }
    
    /**REDUNDANT - Load a text file into a Matrix*/
    public MatrixArray load_2D_Array(String filename)
    {
        BufferedReader in = null;
        String templine;
        String[] array;
        int i = 0;
        try
        {
            //Create file readers
            FileReader f_reader= new FileReader ("input_array.txt");
            in = new BufferedReader(f_reader);
            
            //Set up array by getting sizes
            templine = in.readLine();
            array = templine.split(" ");
            MatrixArray newmatrix = new MatrixArray(Integer.parseInt(""+ array[0]), Integer.parseInt(""+ array[1]));
            
            //While no more lines, read in new line and append to one string
            while((templine = in.readLine()) != null)
            {
                array = templine.split(" ");
                for(int j = 0; j < array.length; j++)
                {
                    newmatrix.setCell(i, j, Integer.parseInt(""+array[j]));
                }
                i++;
            }
            return newmatrix;
        }
        catch (Exception e)
        {
            System.err.println("Error " + e);
        }
        return null;
    }
    
    /**Retrieve a value from a cell*/
    double getCell(int row, int col)
    {
        return grid[row][col];
    }
    
    /**Set a value of a cell*/
    void setCell(int row, int col, double data)
    {
        grid[row][col] = data;
    }

    /**Delete the value in a cell*/
    void deleteCell(int row, int col)
    {
        grid[row][col] = 0;
    }
    
    /**Returns the Type of Matrix*/
    String getType()
    {
        return "MatrixArray";
    }
}
